package org.ruixue.service;

import java.util.Date;
import java.util.List;

import org.ruixue.dao.oms.ProjectManagerMapper;
import org.ruixue.dao.oms.ShopMapper;
import org.ruixue.dao.yshop.UserEnterMapper;
import org.ruixue.entity.oms.ProjectManager;
import org.ruixue.entity.oms.Shop;
import org.ruixue.entity.yshop.UserEnter;
import org.ruixue.entity.yshop.UserEnterExample;
import org.ruixue.response.BaseResponse;
import org.ruixue.util.ByteUtil;
import org.ruixue.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("shopService")
public class ShopService {

	/**
	 * OMS：网店表
	 */
	@Autowired
	private ShopMapper shopMapper;
	
	/**
	 * OMS：项目表
	 */
	@Autowired
	private ProjectManagerMapper projectManagerMapper;

	/**
	 * 平台商场：商户申请表
	 */
	@Autowired
	private UserEnterMapper userEnterMapper;

	protected static final String SHOP_TYPE = "商城";
	protected static final String VALUE_NO = "n";
	protected static final String VALUE_YES = "y";
	protected static final String ENT_ID = "51f17f6ddbae11e68c4d001e67c7ec3a";

	/**
	 * 网店（商户）上个月内
	 *
	 * @return
	 * @throws Exception
	 */
	public BaseResponse downloadYiShop() throws Exception {
		// 获取当前时间戳
		Integer startTime = DateUtil.getTimeInMillis(-2);
		Integer endTime = DateUtil.getTimeInMillis(null);

		UserEnterExample ueExample = new UserEnterExample();
		ueExample.createCriteria().andAddTimeBetween(startTime, endTime);
		List<UserEnter> userEnterList = userEnterMapper.selectByExample(ueExample);

		for (int i = 0; i < userEnterList.size(); i++) {
			UserEnter userEnter = userEnterList.get(i);
			// 查询网店是否存在
			Shop shop = shopMapper.selectByPrimaryKeyAppKey(userEnter.getId().toString());
			if (shop != null) {
				// 修改
				shop.setShopName(userEnter.getMerchantName());
				shop.setDeleted(ByteUtil.byteToInt(userEnter.getIsDel()) == 0 ? "n" : "y");
				shopMapper.updateByPrimaryKeySelectiveShop(shop);
			} else {
				// 获取项目表中一条数据
				List<ProjectManager> projectManagerList=projectManagerMapper.selectProjectManagerList();
				shop = new Shop();
				shop.setShopId(java.util.UUID.randomUUID().toString().replaceAll("-", ""));
				shop.setValidate(VALUE_YES);
				shop.setEnable(VALUE_YES);
				shop.setNeedSplitOrder(VALUE_NO);
				shop.setShopName(userEnter.getMerchantName());
				shop.setShopAccount(userEnter.getMerchantName());
				shop.setEntId(ENT_ID);
				if(projectManagerList.size()>0) {
					shop.setPrjId(projectManagerList.get(0).getPrjId());
					shop.setPrjName(projectManagerList.get(0).getPrjName());
				}
				shop.setAppKey(userEnter.getId().toString());
				shop.setShopType(SHOP_TYPE);
				shop.setCreateTime(new Date());
				shop.setModifyTime(new Date());
				shop.setStartTime(new Date());
				shop.setExpireTime(DateUtil.formatStringToDate("9999-01-01","yyyy-MM-dd"));
				shop.setDeleted(ByteUtil.byteToInt(userEnter.getIsDel()) == 0 ? "n" : "y");
				// 判断是否启动
				if (ByteUtil.byteToInt(userEnter.getIsLock()) == 0 && ByteUtil.byteToInt(userEnter.getIsDel()) != 1) {
					// 添加
					shopMapper.insertSelectiveShop(shop);
				}
			}
		}
		return new BaseResponse();
	}
}
